#ifndef _APPS_BDESKTOP_CROOTWINDOW_H_
#define _APPS_BDESKTOP_CROOTWINDOW_H_

#include "auto.h"
#include <buola/utility/usignal.h>

#include <buola/gui/cwindow.h>
#include <buola/gui/x11/cx11dispatcher.h>

namespace buola { namespace bdesktop {

class CWMDesktop
{
public:
    std::wstring mName;
    CIntPoint mViewPort;
    CIntRect mWorkArea;
};

class CRootWindow : public gui::CWindow, public gui::x11::CX11EventFilter
{
public:
    CRootWindow();
    ~CRootWindow();

    void Create();
    void Init();

    CClient *CreateClient(Window);
    void RemoveClient(CClient*);
    int GetClientCount()            {   return mClients.size();     }
    CClient *GetClientIndex(int i)  {   return mClients[i];             }
    CClient *GetClient(Window);

    CClient *GetActiveClient()      {   return mActiveClient;           }

    int GetActiveDesktop()          {   return mActiveDesktop;          }
    const CSize &GetDesktopSize()   {   return mDesktopSize;            }
    int GetDesktopCount()           {   return mDesktops.size();    }
    CWMDesktop *GetDesktop(int i)   {   return mDesktops[i].get();           }

    void SwitchToDesktop(int);

protected:
    void ReadClientList();
    void ReadActiveWindow();
    void ReadDesktopInfo();

//Events
    virtual bool OnX11Event(XEvent*);

    virtual void DoProperty(XPropertyEvent*);

public:
    USignal<void(CClient*)> sClientAdded;
    USignal<void(CClient*)> sClientRemoved;
    USignal<void(CClient*,CClient*)> sClientActivated;
    USignal<void()> sStackChange;
    USignal<void()> sDesktopChange;
    USignal<void(CClient*)> sClientChange;
    USignal<void(CClient*)> sClientGeometry;

private:
    std::vector<CClient*> mClients;

    CClient *mActiveClient;

    int mActiveDesktop;
    std::vector<std::unique_ptr<CWMDesktop>> mDesktops;
    CSize mDesktopSize;

    friend class CClient;
};

extern CRootWindow *gRootWindow;

/*namespace bdesktop*/ } /*namespace buola*/ }

#endif
